module.exports = {
	// app端因为地图组件需要用到nvue页面
	// 但是nvue无法直接取main文件配置的全局参数 所以使用APP.vue进行中转
	// 组件created中 或者 或者页面 onload中添加该方法进行初始化
	// 初始化COMMON_VAR change_var 以及设置重置页面数据，或者执行初始化回调

	async init_pagedata() {
		let _this = this;
		let _config = this.com_set_config();
		_this.change_var = this.deepclone(_config.main_change_var);
		let { windowHeight } = await uni.getSystemInfoSync();
		_this.change_var.vh = windowHeight / 100;
		_this.change_var.win_height = _this.pxTorpx(windowHeight);
		_this.$forceUpdate();

		setTimeout(() => {
			typeof _this.init_pagedata_callback === 'function' ? _this.init_pagedata_callback() : false;
		}, 100);
	},

	// 初始化状态
	// 1.用户是否登录  2.是否连接设备
	// page_login_judge 初始化完成
	com_page_show(_this) {
		getApp().globalData._this = _this;
		// _this.update_pagedevice();
	},

	// 已连接设备更新到页面
	UPDATE_STATE_CARINFO(data) {
		let _this = this;
		_this.store_set_state_config({ state_name: 'CAR_INFO_CONFIG', data });
	},
	reset_carinfo() {
		uni.removeStorageSync('bike_cc_id');
		uni.removeStorageSync('bike_battery_mode');
		uni.removeStorageSync('bike_sound_setup');
		uni.removeStorageSync('bike_battery_id');
		uni.removeStorageSync('controller_mode_id');
	},
	clear_user(callback = 'refresh') {
		let _this = this;
		this.reset_carinfo();
		this.com_update_log_scan_warn(`清除用户信息：${callback}`);
		this.clear_bluetooth_cache();
		this.reset_bluetooth_cache({ ALL_CLEAR: true });
		uni.removeStorageSync('usertoken');
		uni.removeStorageSync('userid');
		uni.removeStorageSync('openid');
		uni.removeStorageSync('is_normal_user');
		uni.removeStorageSync('select_address_id');
		_this.store_set_state_config({ state_name: 'USER_MSG', data: {} });
		_this.store_set_state({ state_name: 'USER_LOGIN', state_value: false });
		if (callback == 'refresh') {
			_this.PAGE_CALLBACK();
		} else if (callback == 'go_user') {
			_this.goweburl_f('/pages/user/user');
		}
	},
	PAGE_CALLBACK() {
		let _this = this;
		typeof _this.com_onshow_fun === 'function' ? _this.com_onshow_fun() : false;
	},
	//车辆是否在线
	getcarline(id = false) {
		let _this = this;
		let _config = this.com_set_config();
		return new Promise((resolve, reject) => {
			let bike_cc_id = uni.getStorageSync('bike_cc_id');
			if (id) {
				bike_cc_id = id;
			}
			_this.com_getdata({
				url: '/cc/check',
				id_name: '',
				data: {
					bike_cc_id,
				},
				data_fun(res) {
					if (res.state == 1) {
						if (res.data == '1') {
							// 1 离线
							resolve(false);
						} else if (res.data == '0') {
							// 0 在线
							resolve(true);
						}
					} else {
						resolve(false);
					}
				},
				fail_fun(res) {
					resolve(false);
				},
			});
		});
	},
	com_bind_req(sn) {
		let _this = this;
		if (
			!_this.com_loading({
				status: 1,
				title: '绑定中...',
				throttle: true,
			})
		)
			return;

		_this.com_getdata({
			url: '/cc/bind',
			id_name: 'user_id',
			data: {
				sn,
			},
			data_fun(res) {
				let title = res.msg;
				let icon = 'none';
				let callback = function () {
					typeof _this.pagerefresh == 'undefined' ? false : _this.pagerefresh();
				};
				if (res.state == 1) {
					title = '绑定成功';
					icon = 'success';
					callback = function () {
						_this.change_var.isScanning = false;
						if (res.state == 1) {
							uni.setStorageSync('bike_cc_id', sn);
							_this.goweburl_f('/pages/index/mycar');
						}
					};
				}
				_this.com_loading({
					status: 2,
					title,
					icon,
					throttle: true,
					hide_time: 1000,
					callback,
				});
			},
		});
	},

	com_product_click(event) {
		let _this = this;
		let { type, item } = event;
		if (type == 'good_detail') {
			_this.goweburl_f(`/pagesD/shop/good_details?id=${item.id}`);
		}
		if (type == 'order_detail') {
			_this.goweburl_f(`/pagesD/shop/user_order_detail?id=${item.order_num}`);
		}
		if (type == 'order_detail_goods') {
			_this.goweburl_f(`/pagesD/shop/good_details?id=${item.goods_id}&skuid=${item.rule_id}&number=${item.goods_number}`);
		}
	},
	myToolsClick(e) {
		let { item, pagetype } = e;
		let _this = this;
		if (item.url) {
			_this.goweburl_f(item.url);
			return;
		}

		let value = this.page_form[item.form_key];
		if (!this.objJudge(value)) {
			value = '';
		}
		let btn = {
			confirm: '确认',
			cancel: '取消',
		};
		Object.assign(this.popup_config, item, btn);

		if (this.objJudge(item.content)) {
			this.popup_config.placeholder = '';
		} else if (this.objJudge(item.placeholder)) {
			this.popup_config.content = '';
		}

		this.$set(this.popup_config, 'inp_value', value);
		// this.$set(this.popup_config, 'placeholder', item.placeholder)
		this.$nextTick(() => {
			this.openpopup();
		});
	},

	async myCardFormClick(e) {
		let { item, form } = e;
		// console.log(item, 'mehtod.js----myCardFormClick')
		console.log(e, 'mehtod.js----myCardFormClick');
		let _this = this;
		if (item.state == 'switch') {
			// this.$refs.pagelogin.call_chang_model('edit_pwd');
			_this[form][item.key] = _this[form][item.key] == 1 ? 0 : 1;
			return;
		}

		if (item.url) {
			_this.goweburl_f(item.url);
		} else if (item.key == 'base_customer_phone') {
			_this.customer_wechat();
			// _this.callphone(item.tel);
			// _this.permission_to_check('callphone', () => {
			// 	_this.callphone(item.tel);
			// });
		} else if (item.key == 'cache') {
			// 清除缓存
			let cache = _this[form][item.key];
			if (cache) {
				_this.clear_cache(item);
			} else {
				_this.com_msg('暂无需要清除的缓存~');
			}
		} else if (item.key == 'logoff') {
			// 注销用户
			let f = await this.com_modal({
				title: '注销用户',
				content: ' 您正在进行账户注销操作。注销成功后，账户不可用，数据全部清除，此操作不可逆。',
			});
			if (!f) return;
			_this.logoff_user();
		} else if (item.key == 'password') {
			// 账号安全点击修改密码跳转当前页面pagelogin组件的修改密码功能
			// _this.$refs.pagelogin.call_chang_model('edit_pwd');
			_this.goweburl_f('/pagesA/index/login?type=edit_pwd');
		} else if (item.state == 'switch') {
			_this.$refs.pagelogin.call_chang_model('edit_pwd');
		} else if (item.popup_type) {
			_this.openpopup(item.popup_type);
		}
	},
	async logoff_user() {
		let _this = this;
		if (!_this.com_loading({ status: 1, k: 'nextreq', title: '注销中...', throttle: true })) return;
		let response = await _this.com_getdata({
			url: `/user/logout`,
			id_name: 'user_id',
		});
		_this.com_loading_fun({
			mode: 2,
			k: 'nextreq',
			title: '注销成功',
			hide_time: 300,
			response,
			suc_icon: 'success',
			fail_icon: 'none',
			show_fail_msg: true,
			suc_callback: data => {
				return () => {
					_this.clear_user('go_user');
					_this.goweburl_f('/pages/index/index');
				};
			},
			fail_callback: data => {},
		});
	},
	// 调用地图导航
	myLocation(e) {
		let { latitude, longitude, name, address } = e;
		latitude = parseFloat(latitude);
		longitude = parseFloat(longitude);
		uni.openLocation({
			latitude,
			longitude,
			name,
			address,
			success(res) {
				console.log(res);
			},
			fail(res) {
				console.log(res);
			},
		});
	},
	com_getLocation() {
		let _this = this;
		return new Promise((resolve, reject) => {
			uni.getLocation({
				type: 'gcj02',
				success(res) {
					resolve({
						lat: res.latitude,
						lng: res.longitude,
					});
					// const latitude = res.latitude
					// const longitude = res.longitude
					// const speed = res.speed
					// const accuracy = res.accuracy
					// console.log(res)
				},
				fail(res) {
					console.log(res, 'getLocation-fail');
					resolve(false);
				},
			});
		});
	},
	com_chooseLocation() {
		let _this = this;
		uni.chooseLocation({
			success(res) {
				console.log(res);
				// console.log('位置名称：' + res.name);
				// console.log('详细地址：' + res.address);
				// console.log('纬度：' + res.latitude);
				// console.log('经度：' + res.longitude);
				_this.page_chooseLocation(res);
			},
		});
	},
	// 请求头处理
	header_handle(v) {
		let usertoken = uni.getStorageSync('usertoken');
		v['Authorization'] = usertoken;
		return v;
	},
};
